/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ch.pete.dailyscrumtimer;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.media.AudioManager;
import android.media.ToneGenerator;

/**
 * Helper class to play sounds.
 */

public class SoundGenerator {

    /** The ToneGenerator to play the sound. */
    private final ToneGenerator mToneGenerator;

    /** The AudioManager of the system, null if volume not increased. */
    private final AudioManager mAudioManager;
    
    /** The task executor to set the volume back, null if volume not increased. */
    private final ScheduledExecutorService mScheduleTaskExecutor;

    /**
     * Initialize the tone by the given constants.
     */
    SoundGenerator(final Context context, final boolean increaseVolume) {
        if (increaseVolume) {
            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            mScheduleTaskExecutor = Executors.newScheduledThreadPool(1);
        } else {
            mAudioManager = null;
            mScheduleTaskExecutor = null;
        }
        mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF, ToneGenerator.MAX_VOLUME);
    }

    /**
     * Stops the playback immediately.
     */
    final synchronized void stop() {
        if (mToneGenerator != null) {
            mToneGenerator.stopTone();
        }
    }

    final synchronized void release() {
        mToneGenerator.release();
    }

    /**
     * Plays the created sound.
     * 
     * @param duration the sound is played in milli seconds.
     */
    final synchronized void playSound(final int durationMs) {
        final int streamVolume;
        if (mAudioManager != null) {
            streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_DTMF);
            mAudioManager.setStreamVolume(AudioManager.STREAM_DTMF,
                    mAudioManager.getStreamMaxVolume(AudioManager.STREAM_DTMF), 0);
        } else {
            streamVolume = 0;
        }
        mToneGenerator.startTone(ToneGenerator.TONE_CDMA_DIAL_TONE_LITE,
                durationMs);

        if (mAudioManager != null) {
            mScheduleTaskExecutor.schedule(new Runnable() {
                public void run() {
                    mAudioManager.setStreamVolume(AudioManager.STREAM_DTMF, streamVolume, 0);
                }
            }, durationMs, TimeUnit.MILLISECONDS);
        }
    }

}
